<?php
class Demandsstatuses_model extends CRM_Model{
	
	public function __construct(){
		parent::__construct();
	}
	
	public function GetDS(){
		$this->db->select('_demandsstatuses.rid as rid, _demandsstatuses.code as code,
							_demandsstatuses.demandsstatuses_name as demandsstatuses_name,
							_demandsstatuses.d_order as d_order,
							DATE_FORMAT(_demandsstatuses.modifyDT, \'%d.%m.%Y %H:%i\') as modifyDT, 
							_demandsstatuses.descr as descr, _demandsstatuses.archive');
		$this->db->from('_demandsstatuses');
		if($sort = $this->ciObject->GetSessionData('sort')){
			$this->db->orderby($sort['c'], $sort['r']);
		}
		$query = $this->DBGet('_demandsstatuses');
		if($query->num_rows()>0) return $this->ciObject->GetPagingDS($query->result());
		return array();
	}
	
	public function GetEditDS(){
		$this->db->select('_demandsstatuses.rid as rid, _demandsstatuses.code as code,
							_demandsstatuses.demandsstatuses_name as demandsstatuses_name,
							_demandsstatuses.d_order as d_order,
							DATE_FORMAT(_demandsstatuses.modifyDT, \'%d.%m.%Y %H:%i\') as modifyDT,
							_demandsstatuses.descr as descr, _demandsstatuses.archive');
		$this->db->from('_demandsstatuses');
		$this->db->where(array('rid'=>$this->ciObject->GetSessionData('activerecord')));
		$query = $this->DBGet('_demandsstatuses');
		if($query->num_rows()>0) return $query->row();
		return False;
	}
	
	public function CreateRecord(){
		$insArray = array('code'=>$_POST['code'],
							'd_order'=>$_POST['d_order'],
							'demandsstatuses_name'=>$_POST['demandsstatuses_name'],
							'descr'=>$_POST['descr'],
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);
		$this->db->trans_begin();
		$this->db->insert('_demandsstatuses', $insArray);
		$insRid = $this->db->insert_id();
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return $insRid;
		}		
	}
	
	public function UpdateRecord(){
		$updateArray = array('code'=>$_POST['code'],
							'd_order'=>$_POST['d_order'],
							'demandsstatuses_name'=>$_POST['demandsstatuses_name'],
							'descr'=>$_POST['descr'],
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);
		$this->db->trans_begin();
		$this->db->update('_demandsstatuses', $updateArray, array('rid'=>$this->ciObject->GetSessionData('activerecord')));
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return True;
		}		
	}
	
	public function DeleteRecord(){
		$this->db->trans_begin();
		$this->db->delete('_demandsstatuses', array('rid'=>$this->ciObject->GetSessionData('activerecord')));
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return True;
		}		
	}
}
?>